package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Question;
import cn.wolfcode.trip.base.domain.QuestionComment;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.QuestionCommentQueryObject;
import cn.wolfcode.trip.base.service.IQuestionCommentService;
import cn.wolfcode.trip.base.service.IQuestionService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.JSONUtil;
import cn.wolfcode.trip.base.util.UploadUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/questions")
public class QuestionController {

    @Autowired
    private IQuestionService questionService;

    @Autowired
    private IQuestionCommentService commentService;


    @GetMapping("list")
    public PageInfo list(QueryObject qo){
        qo.setOrderBy("q.createTime desc");
        return questionService.query(qo);
    }

    @PostMapping("ask")
    public JSONResult save(Question entity,MultipartFile file){
        //判断是否上传图片
        if (file != null && file.getSize()>0) {
            //上传图片
            String url = UploadUtil.upload(file);
            //设置图片地址到推荐对象中
            entity.setImgUrl(url);
        }
        questionService.saveOrUpdate(entity);
        return new JSONResult();
    }

    @GetMapping("comment/{questionId}")
    public Map<String,Object> QueryByPrimaryKey(@PathVariable Long questionId){
        return questionService.selectByPrimaryKey(questionId);
    }

    @GetMapping("ask/comment/{questionId}")
    public PageInfo getQuestionComment(QuestionCommentQueryObject qo){
      qo.setOrderBy("qc.createTime desc");
      return commentService.selectByQuestionId(qo);
    }

    @PostMapping("browse/{questionId}")
   public JSONResult updateBrowseNumber(@PathVariable Long questionId){
        questionService.updateBrowseNumber(questionId);
        return new JSONResult();
    }

    @PostMapping("ask/comment")
    public JSONResult saveComment(QuestionComment entity){
        commentService.saveComment(entity);
        return new JSONResult();
    }



}
